package com.zee.example;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.zee.log.ZLog;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

public class ThirdPartyInterceptor implements Interceptor {
    private final Charset UTF8 = Charset.forName("UTF-8");
    private String TAG = "Log";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (request.method().equals("POST")) {
            request = addAndEncryptStoreParams(request);
        }
        Response response = chain.proceed(request);

//        ZLog.i(TAG, "------------ Http Begin --------------\n");
//        ZLog.i(TAG, "0. headers = " + request.headers().toString());
//        ZLog.i(TAG, "1. requestUrl = " + request.url().toString() + "\r\n");
//        ZLog.i(TAG, "2. requestBody = " + printRequestBody(request.body()) + "\r\n");
//        ZLog.i(TAG, "2. responseBody = " + printResponseBody(response.body()) + "\r\n");
//        ZLog.i(TAG, "------------ Http End --------------\n");
        return response;
    }

    private Request addAndEncryptStoreParams(Request request) throws IOException {
        RequestBody requestBody = request.body();
        JsonObject jsonBody = null;
        if (requestBody != null) {
            Buffer buffer = new Buffer();
            requestBody.writeTo(buffer);

            Charset charset = UTF8;
            MediaType contentType = requestBody.contentType();

            if (contentType != null) {
                charset = contentType.charset(UTF8);
            }
            String body = buffer.readString(charset);
            jsonBody = new Gson().fromJson(body, JsonObject.class);
        }
        String newBody = buildJsonRequestParams(jsonBody);
        return request.newBuilder().post(RequestBody.create(requestBody.contentType(), newBody)).build();
    }

    public static String buildJsonRequestParams(Object body) {
        Gson gson = new Gson();
        BaseHttpRequestDataNew data = new BaseHttpRequestDataNew();
        if (body == null) {
            body = new Object();
        }
        data.setParamData(body);
        return gson.toJson(data);
    }

    private String printRequestBody(RequestBody requestBody) throws IOException {
        String body = null;
        if (requestBody != null) {
            Buffer buffer = new Buffer();
            requestBody.writeTo(buffer);

            Charset charset = UTF8;
            MediaType contentType = requestBody.contentType();
            if (contentType != null) {
                charset = contentType.charset(UTF8);
            }
            body = buffer.readString(charset);
        }
        return body;
    }


    private String printResponseBody(ResponseBody responseBody) throws IOException {
        if (responseBody == null) {
            return "";
        }
        BufferedSource source = responseBody.source();
        // Buffer the entire body.
        source.request(Long.MAX_VALUE);
        Buffer buffer = source.buffer();
        Charset charset = Charset.defaultCharset();
        MediaType contentType = responseBody.contentType();
        if (contentType != null) {
            charset = contentType.charset(charset);
        }
        return buffer.clone().readString(charset);
    }

}
